package com.shanzmoo.db.personnel.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.base.constant.Constant;
import com.shanzmoo.base.constant.MemberType;
import com.shanzmoo.db.personnel.entity.MemberInfoEntity;
import com.shanzmoo.db.personnel.entity.WechatConfigData;
import com.shanzmoo.db.personnel.entity.dto.MemberInfoQueryDto;
import com.shanzmoo.db.personnel.entity.dto.MemberInfoSaveDto;
import com.shanzmoo.db.personnel.entity.dto.MemberInfoUpdateDto;
import com.shanzmoo.db.personnel.entity.vo.MallMemberInfoVo;
import com.shanzmoo.db.personnel.entity.vo.MemberInfoPageVo;
import com.shanzmoo.db.personnel.entity.vo.MemberInfoVo;
import com.shanzmoo.db.personnel.entity.vo.MemberSelectVo;
import com.shanzmoo.db.personnel.mapper.MemberInfoMapper;
import com.shanzmoo.db.personnel.service.IMemberAddressService;
import com.shanzmoo.db.personnel.service.IMemberInfoService;
import com.shanzmoo.db.personnel.service.IMemberInvoiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 会员信息 服务实现类
 * </p>
 *
 * @author Gzhao
 * @since 2020-12-14
 */
@Slf4j
@Service
public class MemberInfoServiceImpl extends ServiceImpl<MemberInfoMapper, MemberInfoEntity> implements IMemberInfoService {

    @Autowired
    private IMemberAddressService memberAddressService;
    @Autowired
    private IMemberInvoiceService memberInvoiceService;

    @Override
    public SResponse<List<MemberSelectVo>> getSelectList() {
        List<MemberInfoEntity> list = baseMapper.selectList(
                new QueryWrapper<MemberInfoEntity>()
                        .eq("deleted", false)
                        .orderByDesc("id")
        );
        List<MemberSelectVo> reList = list.stream()
                .map(MemberSelectVo::create)
                .collect(Collectors.toList());

        return SResponse.ok(reList);
    }

    @Override
    public SResponse<Boolean> add(MemberInfoSaveDto saveDto) {
        baseMapper.insert(saveDto.convert());

        return SResponse.ok(Boolean.TRUE);
    }

    @Override
    public SResponse<PageRespBase<MemberInfoPageVo>> getPageList(MemberInfoQueryDto queryDto) {
        Page page = PageHelper.startPage(queryDto.getPageNum(), queryDto.getPageSize());

        List<MemberInfoPageVo> list = baseMapper.getPageList(queryDto);

        PageRespBase<MemberInfoPageVo> re = PageRespBase.create(page, list);
        PageHelper.clearPage();
        return SResponse.ok(re);
    }

    @Override
    public SResponse<MemberInfoVo> getInfo(Integer id) {
        MemberInfoEntity member = baseMapper.selectById(id);
        MemberInfoVo infoVo = JSON.parseObject(JSON.toJSONString(member), MemberInfoVo.class);
        return SResponse.ok(infoVo);
    }

    @Override
    public SResponse<MemberInfoVo> getInfoErp(Integer id) {
        MemberInfoVo memberInfoVo = baseMapper.getInfoById(id);
        return SResponse.ok(memberInfoVo);
    }

    @Override
    public SResponse<Boolean> edit(MemberInfoUpdateDto updateDto) {
        MemberInfoEntity old = baseMapper.selectById(updateDto.getId());

        MemberInfoEntity update = updateDto.convert(old);

        // 企业用户修改手机号后需重新绑定微信
        if (old.getType() == MemberType.COMPANY){
            if (!StrUtil.equals(old.getMobile(), update.getMobile())){
                update.setStatus(false);
            }
        }

        baseMapper.updateById(update);
        return SResponse.ok(Boolean.TRUE);
    }

    @Override
    public MallMemberInfoVo getMemberInfo(Integer memberId) {
        MallMemberInfoVo memberInfo = MallMemberInfoVo.create(baseMapper.selectById(memberId));

        memberInfo.setDefaultAddress(memberAddressService.getDefaultAddress(memberId));

        memberInfo.setDefaultInvoice(memberInvoiceService.obtianInvoice(String.valueOf(memberId)));

        return memberInfo;
    }

}
